package com.smforj.ssm.modules.sys.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.smforj.ssm.frame.core.enums.RouterMapping;
import com.smforj.ssm.frame.core.utils.StringEx;
import com.smforj.ssm.frame.core.web.controller.BaseControllerSupportImpl;
import com.smforj.ssm.frame.core.web.domain.DataResult;
import com.smforj.ssm.frame.core.web.domain.DataResult.DataStatus;
import com.smforj.ssm.modules.api.domain.UserCache;
import com.smforj.ssm.modules.lib.domain.IUser;
import com.smforj.ssm.modules.sys.service.LoginService;

@RouterMapping("")
@Controller
@RequestMapping(value={"/${adminPath}"})
public class _LoginController extends BaseControllerSupportImpl{  
	
	@Autowired
	private LoginService loginService; 
	
	/***
	 * 注册
	 * @return
	 * @date 2016-8-11 下午2:12:06
	 */
	@RequestMapping(value={"login"},method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) 
	public ModelAndView gologin(ModelMap model)
	{ 
		if(logger.isDebugEnabled())
			logger.debug("gologin=============================");   
		return new ModelAndView(this.getViewPath("login"));
	}
	
	/***
	 * 登陆
	 * @return
	 * @date 2016-8-11 下午2:12:06
	 */
	@RequestMapping(value={"login"},method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE) 
	public ModelAndView dologin(ModelMap model)
	{ 
		if(logger.isDebugEnabled())
			logger.debug("dologin=============================begin");  
		ModelAndView mav = new ModelAndView();  
		String username = request.getParameter("username");
		String password = request.getParameter("password"); 
		String ip = request.getRemoteAddr();
		if(StringEx.isExEmpty(username) || StringEx.isExEmpty(password))
		{
			model.addAttribute("message", "用户名或密码错误!!!");
			model.addAttribute("flag", true);
			mav.setViewName(this.getViewPath("login"));
			return mav;
		}
		if(logger.isDebugEnabled())
			logger.debug(String.format("username %s password %s ip %s ",username,password,ip)); 
		UserCache uc = loginService.login(username, password, ip);
		if(uc != null)
		{ 
			//加载权限信息
			//###############################################
			if(logger.isDebugEnabled())
				logger.debug("dologin=============================uc"); 
			//###############################################
			model.addAttribute("flag", false);
			model.addAttribute("message", "success");
			this.session.setAttribute("manager", uc.getUser());  
			mav.setViewName(this.getRedirectPath(adminPath + "/index"));
		}else
		{ 
			model.addAttribute("flag", true);
			model.addAttribute("message", "用户名或密码错误!!!");
			mav.setViewName(this.getViewPath("login"));
		}
		if(logger.isDebugEnabled())
		{
			logger.debug("UserCache:"+uc);  
			logger.debug("dologin=============================end");  
		}
		return mav;
	}
	
	/***
	 * 登陆
	 * @return
	 * @date 2016-8-11 下午2:12:06
	 */
	@RequestMapping(value={"loginjson"},method = RequestMethod.POST, 
			produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public DataResult<IUser> dologinjson(ModelMap model)
	{ 
		if(logger.isDebugEnabled())
			logger.debug("dologinjson=============================begin");  
		DataResult<IUser> result = new DataResult<IUser>();  
		String username = request.getParameter("username");
		String password = request.getParameter("password"); 
		String ip = request.getRemoteAddr();
		if(StringEx.isExEmpty(username) || StringEx.isExEmpty(password))
		{
			result.setStatus(DataStatus.ERROR);
			result.setMessage("用户名或密码为空!!!");
			return result;
		}
		
		if(logger.isDebugEnabled())
			logger.debug(String.format("username %s password %s ip %s ",username,password,ip)); 
		UserCache uc = loginService.login(username, password, ip);
		if(uc != null)
		{ 
			//加载权限信息
			model.addAttribute("message", "success");
			model.addAttribute("manager", uc.getUser());
			result.setStatus(DataStatus.OK);
		}else
		{
			model.addAttribute("message", "用户名或密码错误!!!");
			result.setStatus(DataStatus.ERROR);
			result.setMessage("用户名或密码错误!!!");
		}
		if(logger.isDebugEnabled())
		{
			logger.debug("UserCache:"+uc);  
			logger.debug("dologinjson=============================end");  
		}
		return result;
	}
	
	/***
	 * sign up
	 * @return
	 * @date 2016-8-11 下午2:12:06
	 */
	@RequestMapping(value={"logout"},method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE) 
	public ModelAndView logout()
	{ 
		if(logger.isDebugEnabled())
			logger.debug("logout=============================begin");  
		ModelAndView mav = new ModelAndView();  
		String sessionid = this.request.getParameter("sessionid");   
		loginService.logout(sessionid); 
		mav.setViewName(this.getViewPath("login"));
		if(logger.isDebugEnabled())
			logger.debug("logout=============================end");  
		return mav;
	}
}
